package org.ww.seckill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ww.common.utils.R;
import org.ww.seckill.service.SeckillService;
import org.ww.seckill.to.SeckillSkuRedisTo;

import java.util.List;

/**
 * @program: gmall
 * @description:
 * @author: ww
 * @create: 2024-11-07 14:17
 **/

@Controller
public class SeckillSkuController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 返回当前时间可以秒杀的商品信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getCurrentSeckillSkus")
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> redisToList = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(redisToList);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SeckillSkuRedisTo redisTo = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(redisTo);
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        String result = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",result);
        return "success.html";
    }
}
